<template>


    <div class="page-box">
       <div class="page-title">
          {{ '出库记录' }}
       </div>
 
       <div class="page-search">
          <div class="search-box flex_start_center" :style="{ height: IsHeight }">
             <el-input v-model="pageData.keywords" placeholder="出库单号/订单类型/关联单号"
             @input="getAlltabeData"   style="width: 200px; margin-right: 10px; margin-bottom: 10px;" />

          </div>
 
          <div class="search-btn">
             <div class="btn_item" @click="handleClickHeight">
                <img class="btn_img" src="@/assets/sx.png" alt=""> <span> 高级筛选 </span>
             </div>
             <div class="btn_item" @click="handleExport">
                <img class="btn_img" src="@/assets/dc.png" alt=""> <span> 导出 </span>
             </div>
  
          </div>
 
       </div>
       <div class="search-btn-right">
       
       </div>
    </div>
 
    <div class="table-box">
 
       <el-table :data="tabeData" style="width: 100%" v-loading="loading" border
           default-expand-all    >
       
          <el-table-column align="center" prop="no" label="出库单号" width="210">
            <template #default="scope">
              <div style="color: blue; cursor: pointer;" @click="handleClickDetails(scope.row)">{{ scope.row.no }}</div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="source_type_text" label="订单类型" width="100" />
          <el-table-column align="center" prop="source_no" label="关联出库单/采购退货单" />
          <el-table-column align="center" prop="out_date" label="出库日期" width="180" />
          <el-table-column align="center" prop="customer_name" label="供应商/客户" width="120" />
          <el-table-column align="center" prop="contact_name" label="出库/采购退货负责人" width="180" />
          <el-table-column align="center" prop="contact_mobile" label="联系电话" width="120" />
          <el-table-column align="center" prop="note" label="备注" width="120" />
          <el-table-column align="center" prop="creator_name" label="创建人" width="120" />
          <el-table-column align="center" prop="created_at" label="创建时间" width="180" />

       </el-table>
    </div>
 
    <!-- 分页 -->
    <Pagination class="pagination" @pageSize="pageSize" @getPage="getPage" :page="pageData.page"
       :page-size="pageData.page_size" :total="total" />
    <!-- 详情 -->
    <Details v-model:dialogVisible="DetailsVisible" :dialogRow="dialogRow" @submit="handleSubmit" />
 </template>
 
 <script setup lang="ts">

 import { ref, onMounted, watch } from "vue";
 import { exportTableToExcel, formatters } from "@/utils/exportTable";
 import api from "@/api";

 import Pagination from "@/components/Pagination.vue";

 import Details from "./components/OutboundRecord/Details.vue";

 // 定义仓库数据类型
 interface Warehouse {
    id: string | number;
    no: string; // 出库单号
    source_id: number; // 来源订单ID
    source_type: string; // 来源订单类型
    source_no: string; // 来源订单编号
    out_id: number; // 出库ID
    out_date: string; // 出库日期
    note: string; // 出库备注
    creator_admin_id: number; // 创建人/出库人 ID
    created_at: string; // 创建时间
    creator_id: number; // 创建者ID
    creator_name: string; // 创建者名称
    source_type_text: string; // 来源订单类型说明
    creator_admin_name: string; // 创建人/出库人姓名
    customer_id: number; // 客户ID
    customer_name: string; // 客户名称
    contact_id: number; // 负责人ID
    contact_name: string; // 负责人姓名
    contact_mobile: string; // 负责人电话
 }
 const DetailsVisible = ref<boolean>(false);
 let IsHeight = ref<number | string>(0);
 const search = ref<string>('');
 let dialogRow = ref<Warehouse>({} as Warehouse);
 const pageData = ref({
    page_size: 10,
    page: 1,
    keywords: "",
 });
 const total = ref(0);
 const tabeData = ref<Warehouse[]>([]);


 const handleClickDetails =  async (row: Warehouse) => {

   const res = await api.product.warehouse_product_out_record_detail({
    id: row.id
   });
   if(res.code == 200){
    dialogRow.value = res.data;
    DetailsVisible.value = true;
   }
   
 }

 let nub = ref<number>(0);
 const handleClickHeight = () => {
    nub.value++;
    if (nub.value % 2 == 1) {
       IsHeight.value = '34px';
    } else {
       IsHeight.value = '0px';
    }
 }
 
 const pageSize = (e: number) => {
    pageData.value.page_size = e;
    getAlltabeData();
 }
 const getPage = (e: number) => {
    pageData.value.page = e;
    getAlltabeData();
 }
 onMounted(() => {
    getAlltabeData();
 })
 

 // 获取仓库列表
 const getAlltabeData = async () => {
    loading.value = true;
    try {
      const res = await api.product.warehouse_product_out_record_list({
         ...pageData.value,
      });
      tabeData.value = res.data.data;
      total.value = res.data.pager.total_records;

    } catch (error) {
      console.error('获取出库记录列表失败', error);
    } finally {
      loading.value = false;
    }
 };
 
 const loading = ref(false);

 

 // 提交表单回调
 const handleSubmit = () => {
    getAlltabeData();
 }

 const handleExport = () => {
  exportTableToExcel({
    data: tabeData.value,
    fileName: '出库记录', //文件名
    sheetName: '出库记录', //工作表名
    //导出字段配置
    fields: [
      { field: 'no', title: '出库单号' },
      { 
        field: 'source_type_text', 
        title: '订单类型'
      },
      { field: 'source_no', title: '关联出库单/采购退货单' },
      { 
        field: 'out_date', 
        title: '出库日期',
        formatter: formatters.formatDate
      },
      { field: 'customer_name', title: '供应商/客户' },
      { field: 'contact_name', title: '出库/采购退货负责人' },
      { field: 'contact_mobile', title: '联系电话' },
      { field: 'note', title: '备注' },
      { field: 'creator_name', title: '创建人' },
      { 
        field: 'created_at', 
        title: '创建时间',
        formatter: formatters.formatDate
      }
    ]
  });
 };

 </script>
 
 <style lang="scss" scoped>
 .btn-box {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 5px;
 }
 
 .main-warehouse {
    font-weight: bold;
    color: #333;
 }
 
 // 输入框样式
 :deep(.el-input__wrapper) {
    background-color: rgba(250, 252, 251, 1);
 }
 
 //表格样式
 :deep(.el-table) {
    --el-table-header-bg-color: rgba(235, 236, 240, 1);
 }
 
 :deep(.el-table thead) {
    color: #444;
 }
 
 // 子仓库行样式
 :deep(.sub-warehouse-row) {
    background-color: #fef6e5;
 }
 
 //分页样式
 .pagination {
    width: calc(100% - 250px);
    position: fixed;
    bottom: 20px;
    right: 20px;
    padding-top: 10px;
    border-top: 1px solid #98989848;
 }
 
 :deep(.el-pagination.is-background .el-pager li.is-active) {
    background-color: #000 !important;
 }
 </style>
 